<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <h2 class="login-title">后台管理系统登录</h2>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="80px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/modules/user'
import type { FormInstance, FormRules } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref<FormInstance>()

// const handleLogin1 = async () => {
//   try {
//     await userStore.login(loginForm)
//     router.push('/home')
//   } catch (error) {
//     console.error('登录失败:', error)
//   }
// }
const loading = ref(false)

// 登录表单数据
const loginForm = ref({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = ref<FormRules>({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
})

// 处理登录
const handleLogin = async () => {
  try {
    // 验证表单
    await loginFormRef.value?.validate()
    
    loading.value = true
    
    // 模拟登录请求
    setTimeout(() => {
      // 这里应该调用实际的登录API
      // 登录成功后设置token和用户信息
      const mockToken = 'mock-jwt-token'
      const mockUserInfo = {
        id: 1,
        username: loginForm.value.username,
        avatar: '/avatar.png',
        roles: ['admin'],
        permissions: ['*']
      }
      
      userStore.setToken(mockToken)
      userStore.setUserInfo(mockUserInfo)
      
      loading.value = false
      
      // 跳转到首页
      router.push('/homePage')
    }, 1000)
  } catch (error) {
    console.error('登录失败:', error)
    loading.value = false
  }
}
</script>

<style scoped lang="less">
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f7fa;
}

.login-form-wrapper {
  background-color: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  width: 400px;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #304156;
}

.login-form {
  .login-btn {
    width: 100%;
  }
}
</style>